package SpilitFileProject;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


public class SplitFileGUI {
    public static void main(String[] args){
        createGUI();
    }
    private static void createGUI(){
        JFrame jf = new JFrame("文件拆分合并器");
        Font font = new Font("宋体",0,35);
        jf.setSize(1200,800);
        jf.setLocationRelativeTo(null);
        JPanel jp = new JPanel(new GridLayout(4,1));
        JPanel selecjp = new JPanel();
        jp.add(selecjp);
        jf.setContentPane(jp);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //选择操作
        JRadioButton jrb1 = new JRadioButton("拆分文件");
        JRadioButton jrb2 = new JRadioButton("合并文件");
        jrb1.setSelected(true);
        jrb1.setFont(font);
        jrb2.setFont(font);
        ButtonGroup bg = new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);

        selecjp.add(jrb1);
        selecjp.add(jrb2);

        //选择路径
        JPanel pathSelect = new JPanel(new GridLayout(2,1));
        jp.add(pathSelect);
        String[][] datas = {};
        String[] titles = {"已选择的文件："};
        DefaultTableModel model = new DefaultTableModel(datas,titles);
        JTable seleTable = new JTable(model);
        JFileChooser jfc = new JFileChooser();
        jfc.setMultiSelectionEnabled(true);//设置可多选文件
        JScrollPane seleScro = new JScrollPane(seleTable);
        pathSelect.add(seleScro);
        //按钮
        JPanel buttonP = new JPanel();
        JButton jb = new JButton("选择文件");
        JLabel sizejl = new JLabel("请输入拆分大小(kB):");
        JTextField jtf = new JTextField("2");
        sizejl.setFont(font);
        jtf.setFont(font);
        buttonP.add(jb);
        buttonP.add(sizejl);
        buttonP.add(jtf);
        JPanel buttonP2 = new JPanel();
        JButton jb2 = new JButton("拆分/合并文件");
        buttonP2.add(jb2);
        JPanel button = new JPanel(new GridLayout(2,1));
        button.add(buttonP);
        button.add(buttonP2);
        jp.add(button);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.setRowCount(0);//行数设置为0，相当于清空
                int returnVal = jfc.showOpenDialog(jp);
                File[] files = jfc.getSelectedFiles();
                if(returnVal==JFileChooser.APPROVE_OPTION){
                    for(File f:files){
                        model.addRow(new String[]{f.getAbsolutePath()});
                    }
                }
            }
        });
        JTextArea jta = new JTextArea();
        jta.setFont(font);
        jta.setText("返回消息如下:\n");
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(jrb1.isSelected()){
                    //拆分
                    String path = (String) seleTable.getValueAt(0,0);
                    jta.append("拆分:"+path+"\n");
                    File f = new File(path);
                    int perSize = Integer.parseInt(jtf.getText())*1024;//2KB
                    new SplitFileMethod().splitFile(f,perSize);
                    jta.append("拆分成功，生成文件在:"+f.getParentFile().getAbsolutePath()+"\n");

                }else if(jrb2.isSelected()){
                    //合并
                    String[] paths = new String[seleTable.getRowCount()];
                    for(int i=0;i<seleTable.getRowCount();i++){
                        paths[i]=(String) seleTable.getValueAt(i,0);
                    }
                    new SplitFileMethod().mergeFile(paths);
                    jta.append("合并成功，生成文件在:"+paths[0].substring(0,paths[0].length()-2)+"\n");
                }
            }
        });
        JScrollPane resMes = new JScrollPane(jta);
        jp.add(resMes);

        jf.setVisible(true);
    }
}
